<?php

namespace Controller;

use Extension\Tools;
use Handler\UserHandler;
use Handler\ZoneHandler;
use Model\User;

class ZoneController extends BaseController {
	/**
	 * 获取大区列表
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_GetZoneList($clientId, $json) {
		$recNo = $json->recno;
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, json_encode ( ZoneHandler::mb_GetInstance ()->mb_GetZones () ) );
	}
	/**
	 * 获取大区房间列表
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_GetRoomList($clientId, $json) {
		$recNo = $json->recno;
		$zoneId = $json->zoneid;
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, json_encode ( ZoneHandler::mb_GetInstance ()->mb_GetRooms ( $zoneId ) ) );
	}
	/**
	 * 登录
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_Login($clientId, $json) {
		$recNo = $json->recno;
		$name = $json->name;
		$user = UserHandler::mb_GetInstance ()->mb_GetUserForName ( $name );
		if (isset ( $user )) {
			$user->mb_ReInit ( $clientId );
			if ($user->m_RoomId != - 1)
				ZoneHandler::mb_GetInstance ()->mb_ReEnterRoom ( $user, $user->m_RoomId );
		} else {
			$repData = UserHandler::mb_GetInstance ()->mb_AddUser ( $name, $clientId );
			if ($repData !== true) {
				$this->mb_OutTextMessage ( $clientId, $recNo, $repData->m_Result, $repData->m_Data );
				return;
			}
			$user = UserHandler::mb_GetInstance ()->mb_GetUserForName ( $name );
		}
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, json_encode ( $user->mb_GetData () ) );
		Tools::mb_LogResult ( "$user->m_Name Login!" );
	}
	/**
	 * 重连
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_ReConnect($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		$user->mb_ReInit ( $clientId );
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, ErrMsg_Normal );
		Tools::mb_LogResult ( "$user->m_Name ReLogin!" );
	}
	/**
	 * 心跳
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_HeartBeat($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		$user->m_HeartBeat = time ();
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 登出
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_Logout($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		if ($user->m_RoomId != - 1) {
			if (ZoneHandler::mb_GetInstance ()->mb_ExitRoom ( $user->m_RoomId, $user )) {
				$repData = UserHandler::mb_GetInstance ()->mb_RemoveUser ( $user->m_Name );
				if ($repData != true) {
					$this->mb_OutTextMessage ( $clientId, $recNo, $repData->m_Result, $repData->m_Data );
					return;
				}
			}
		} else {
			$repData = UserHandler::mb_GetInstance ()->mb_RemoveUser ( $user->m_Name );
			if ($repData != true) {
				$this->mb_OutTextMessage ( $clientId, $recNo, $repData->m_Result, $repData->m_Data );
				return;
			}
		}
		
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, ErrMsg_Normal );
		Tools::mb_LogResult ( "$user->m_Name Logout!" );
	}
	/**
	 * 进入大区
	 *
	 * @param string $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_EnterZone($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		$zoneId = $json->zoneid;
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		if ($user->m_RoomId == - 1) {
			$zone = ZoneHandler::mb_GetInstance ()->mb_GetZones () [$zoneId - 1];
			if ($user->m_Coin < $zone ['minenter']) {
				$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, '您的金币不足，请稍后再试' );
				return;
			}
			if ($user->m_Coin > $zone ['maxenter']) {
				$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, '您的金币数超过限制，请选择更高级大区' );
				return;
			}
			
			if (ZoneHandler::mb_GetInstance ()->mb_EnterRoom ( $user, $zoneId )) {
				$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, '进入大区成功' );
			} else {
				$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, '进入大区失败' );
			}
		} else {
			ZoneHandler::mb_GetInstance ()->mb_ReEnterRoom ( $user, $user->m_RoomId );
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, '进入大区成功' );
		}
	}
	/**
	 * 进入房间
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_EnterRoom($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		$roomId = $json->roomid;
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		if ($user->m_RoomId == - 1) {
			$bResult = ZoneHandler::mb_GetInstance ()->mb_ReEnterRoom ( $user, $roomId );
			if ($bResult == false) {
				$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, '金币不足' );
				return;
			}
		} else {
			ZoneHandler::mb_GetInstance ()->mb_ReEnterRoom ( $user, $user->m_RoomId );
		}
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, '进入大区成功' );
	}
	/**
	 * 退出大区
	 *
	 * @param string $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_ExitZone($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		$bResult = ZoneHandler::mb_GetInstance ()->mb_ExitRoom ( $user->m_RoomId, $user );
		
		if ($bResult) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, '退出成功' );
		} else {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, '退出成功' );
		}
	}
	/**
	 * 创建房间
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_CreateRoom($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		$zoneId = $json->zoneid;
		$roomId = $json->roomid;
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		if ($user->m_RoomId != - 1) {
			
			$room = ZoneHandler::mb_GetInstance ()->mb_GetRoom ( $user->m_RoomId );
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Dialog, json_encode ( array (
					( string ) $user->m_RoomId,
					$room->m_RoomName 
			) ) );
			return;
		}
		
		if (ZoneHandler::mb_GetInstance ()->mb_CreateRoom ( $zoneId, $roomId ) == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, '该房号已存在' );
			return;
		}
		
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, $roomId );
	}
	/**
	 * 加入房间
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_JoinRoom($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		$roomId = $json->roomid;
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		if ($user->m_RoomId != - 1 && $user->m_RoomId != $roomId) {
			$room = ZoneHandler::mb_GetInstance ()->mb_GetRoom ( $user->m_RoomId );
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Dialog, json_encode ( array (
					( string ) $user->m_RoomId,
					$room->m_RoomName 
			) ) );
		} else {
			$room = ZoneHandler::mb_GetInstance ()->mb_GetRoom ( $roomId );
			if (isset ( $room )) {
				if(ZoneHandler::mb_GetInstance ()->mb_ReEnterRoom ( $user, $roomId ) == false){
					$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, '金币不足或人数已满' );
					return;
				}
				$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, '进入房间成功' );
			} else {
				$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, '该房号不存在' );
			}
		}
	}
	public function mb_DebugLog($clientId, $json){
		$token = $json->token;
		$recNo = $json->recno;
		$debug = $json->debug;
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		Tools::mb_DebugLog("[ $user->m_Name ] - $debug");
	}
}